using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Repository.Properties;
using System.Reflection;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCRuleConstant : RepositoryItemUI
	{
		#region Initialization

		public UCRuleConstant(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();

			init();
		}
		private void init()
		{
			DataTable dt = Global.Data.GetDataTypes();
			cboDataType.BeginUpdate();
			cboDataType.Items.Clear();
			foreach ( DataRow row in dt.Rows ) {
				cboDataType.Items.Add(row["datatype"].ToString());
			}
			cboDataType.EndUpdate();

		}

		#endregion

		#region Public Members

		#region Properties

		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _const;
			}
			set
			{
				base.RepositoryItem = value;
				if ( value is RuleConstant ) {
					_const = (RuleConstant)value;
					load();
				}
			}
		}

		public override string TypeText
		{
			get { return Resources.sRuleConstant; }
		}

		public override string TypeDescription
		{
			get { return ""; }
		}

		#endregion

		#endregion

		#region Private Members

		#region Variables
		private RuleConstant _const = null;
		#endregion

		#region Methods
		private void load()
		{
			if ( _const == null )
				return;

			txtName.Text = _const.Name;
			txtDesc.Text = _const.Description;
			cboDataType.Text = _const.DataType;
			txtValue.Text = _const.Value;

			if ( string.IsNullOrEmpty(cboDataType.Text) )
				cboDataType.SelectedIndex = cboDataType.FindString("string");

			usedByHierarchyRuleConstants.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
		}
		private void save()
		{
			Type t = Global.Data.GetDOTNETTypeofXSDDataType(cboDataType.Text);
			object o = null;
			try {
				o = Convert.ChangeType(txtValue.Text, t);
			} catch {
				MessageBox.Show(string.Format(
					"The entered value is not of type '{0}'. Please enter a appropriate value.",
					cboDataType.Text), this.TypeText, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				txtValue.Focus();
				txtValue.SelectAll();
				return;
			}
			if ( o != null )
				txtValue.Text = o.ToString();

			if ( _const == null )
				_const = new RuleConstant();

			_const.Reload();

			_const.Name = txtName.Text;
			_const.Description = txtDesc.Text;
			_const.DataType = cboDataType.Text;
			_const.Value = txtValue.Text;
		}
		#endregion

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( e.KeyChar < (char)32 ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		#region Event Handlers
		#endregion

		#endregion

		protected override void NotifyPropertyChange(string property, object value)
		{
			if ( property == "Name" && value != null ) {
				txtName.Text = value.ToString();
			}
		}
	}
}
